package com.incar.chuxing.utils.Dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.incar.chuxing.R;
import com.incar.chuxing.activity.WebViewActivity;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.bean.DayRentPriceBean;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.SPKey;
import com.incar.chuxing.utils.SPUtils;

import java.util.List;

public class ConfirmDayRentDialog extends Dialog {
    public static final int SEND_CAR_DAILY_RENT = 1;
    public static final int NORMAL_DAILY_RENT = 0;
    Context mContext;
    private RecyclerView confirmRc;
    private TextView choose_cancel;
    private TextView choose_ok;
    private DayRentPriceBean.AttachmentBean confirmData;
    private OnNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private OnYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
    private String carGrade;
    private String buttonText;

    public ConfirmDayRentDialog(Context context) {
        super(context, R.style.navi_dialog);
        this.mContext = context;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_confirm_choose_days);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        window.setWindowAnimations(R.style.dialog_choose_navi);
        initData();
        initView();
        initEvent();
    }


    public void setData(DayRentPriceBean.AttachmentBean confirmData) {
        this.confirmData = confirmData;
    }

    public void setCarGrade(String carGrade) {
        this.carGrade = carGrade.replace("+", "PLUS").toUpperCase();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initData() {

    }


    /**
     * 初始化界面控件
     */
    private void initView() {
        choose_cancel = (TextView) findViewById(R.id.confirm_cancel);
        choose_ok = (TextView) findViewById(R.id.confirm_ok);
        confirmRc = findViewById(R.id.confirm_rc);
        confirmRc.setLayoutManager(new LinearLayoutManager(mContext));
        ConfirmPriceAdapter confirmPriceAdapter = new ConfirmPriceAdapter(confirmData.getList());
        confirmRc.setAdapter(confirmPriceAdapter);

        String tips2 = SPUtils.getInstance(mContext).getStringValue(SPKey.DAY_RENT_TIP_BOTTOM, "最终订单金额会根据实际使用情况计算，请详见");
        ((TextView) findViewById(R.id.confirm_tips_two)).setText(tips2);

        findViewById(R.id.confirm_price_rule).setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(mContext, WebViewActivity.class);
            intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, "计价规则");
            intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlContainer.Request.h5UrlMap.get(UrlContainer.Request.html_price_dayRent_level) + "?model=" + carGrade);
            mContext.startActivity(intent);
        });
    }


    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        choose_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    yesOnclickListener.onYesClick(confirmData.getIsNeedRecharge());
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        choose_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }


    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(OnNoOnclickListener onNoOnclickListener) {

        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(OnYesOnclickListener onYesOnclickListener) {
        this.yesOnclickListener = onYesOnclickListener;
    }

    public void setChagableButtonText(String buttonText) {
        this.buttonText = buttonText;
    }


    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface OnYesOnclickListener {
        void onYesClick(int type);
    }

    public interface OnNoOnclickListener {
        void onNoClick();
    }


    class ConfirmPriceAdapter extends BaseQuickAdapter<DayRentPriceBean.AttachmentBean.ListBean, BaseViewHolder> {
        public ConfirmPriceAdapter(List<DayRentPriceBean.AttachmentBean.ListBean> data) {
            super(R.layout.item_confirm_fee, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, DayRentPriceBean.AttachmentBean.ListBean item) {
            helper.setText(R.id.item_title, item.getTitle());
            helper.setText(R.id.item_content, item.getContent());
        }
    }
}